//
//  DHChangePhoneViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2017/12/27.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHChangePhoneViewController.h"

@interface DHChangePhoneViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *latestTF;

@end

@implementation DHChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"修改手机号";
    
    self.phoneTF.text = [NSString hindPhoneString4Char:[GlobalManager shareGlobalManager].user.phone];
    
    [self.codeButton bk_addEventHandler:^(id sender) {
        [[DHNetAPIClient shareDHNetAPIClient] request_smscodeWithPhone:[GlobalManager shareGlobalManager].user.phone AndBlock:^(id data, NSError *error) {
            __block int leftTime = 60;
            self.codeButton.backgroundColor = [UIColor colorWithHexString:@"#cccccc"];
            [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.codeButton setTitle:[NSString stringWithFormat:@"已发送(%is)", leftTime] forState:UIControlStateDisabled];
            self.codeButton.layer.borderColor = [UIColor colorWithHexString:@"#cccccc"].CGColor;
            self.codeButton.enabled = NO;
            [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer) {
                if (leftTime == 0) {
                    self.codeButton.enabled = YES;
                    [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                    [self.codeButton setTitleColor:[UIColor colorWithHexString:@"#4a4a4a"] forState:UIControlStateNormal];
                    self.codeButton.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
                    //计时完成，销毁定时器
                    [timer invalidate];
                }
                leftTime--;
                [sender setTitle:[NSString stringWithFormat:@"已发送(%is)", leftTime] forState:UIControlStateDisabled];
            } repeats:YES];
        }];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)ensureAction:(id)sender {
    if (![self.latestTF.text isValidPhone]) {
        [NSObject showHudTipStr:@"请输入正确的手机号码"];
        return;
    }
    if (self.codeTF.text.length == 0) {
        [NSObject showHudTipStr:@"请输入验证码"];
        return;
    }else{
        [[DHNetAPIClient shareDHNetAPIClient] request_user_phone_modifyWithParams:@{@"smscode":self.codeTF.text,@"new_phone":self.latestTF.text,@"old_phone":[GlobalManager shareGlobalManager].user.phone} andBlock:^(id data, NSError *error) {
            [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                [NSObject showHudTipStr:@"修改成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
